package com.neveu.android.ds.groupmessenger;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.UriMatcher;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;

public class MessageProvider extends ContentProvider {

	private SharedPreferences messageData;
	private Editor messageEditor;
	private static UriMatcher sUriMatcher = null;
	private Resources res;
		
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri arg0, ContentValues arg1) {
		
		messageEditor.putString(arg1.getAsString("provider_key"), arg1.getAsString("provider_value"));
		messageEditor.commit();
		return null;
	}

	@Override
	public boolean onCreate() {
		res = this.getContext().getResources();
		messageData = this.getContext().getSharedPreferences("messages", 0);
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(res.getString(R.string.PROVIDER_URI), "messages", 1);
		messageEditor = messageData.edit();
		messageEditor.clear();
		return true;
	}

	@Override
	public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
			String arg4) {
		MatrixCursor cursor = new MatrixCursor(new String[]{"provider_key", "provider_value"});
		cursor.addRow(new String[] {arg2, messageData.getString(arg2, "0")});
		return cursor;
	}

	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
